package com.okonna.selenium.tide.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import com.okonna.selenium.tide.Application;
import com.okonna.selenium.tide.eventhandlers.genericevents.ExitListener;
import com.okonna.selenium.tide.eventhandlers.menuevents.OptionsListener;

public class MainView
{
    private Shell shMain;
    private Display display;
    private Application app;

    public MainView(Application tmpApp)
    {
		System.out.println(this.getClass().getSimpleName() + ".MainView(" + tmpApp + ")");
		app = tmpApp;
		display = app.getDisplay();
		shMain = new Shell(display);
    }

    public Shell initView()
    {
		System.out.println(this.getClass().getSimpleName() + ".initView()");
		shMain.setText("Selenium Tide");
		shMain.setImage(new Image(display, "selenium-rc-logo.png"));
		shMain.setSize(640, 480);
		shMain.setLayout(new GridLayout(2, false));
		
		final TabFolder tabFolder = new TabFolder(shMain, SWT.NONE);
		
		final TabInteractiveView swtTabInteractiveView = new TabInteractiveView(tabFolder, app);
		final Composite cmpInteractive = swtTabInteractiveView.initComposite(SWT.BORDER);
		
	    TabItem tiInteractive = new TabItem (tabFolder, SWT.NULL);
	    tiInteractive.setText ("Interactive");

	    TabItem item2 = new TabItem (tabFolder, SWT.NULL);
	    item2.setText ("Test");
		
	    
		tiInteractive.setControl(cmpInteractive);
	
		//Layout menu bar
		
		final Menu mnuMain = new Menu(shMain, SWT.BAR);
	
		final Menu mnuFile = new Menu(shMain, SWT.DROP_DOWN);
		
		final Menu mnuEdit = new Menu(shMain, SWT.DROP_DOWN);
		
		final Menu mnuOptions = new Menu(shMain, SWT.DROP_DOWN);
		
		final Menu mnuHelp = new Menu(shMain, SWT.DROP_DOWN);
		
		final MenuItem mniFileHeader = new MenuItem(mnuMain, SWT.CASCADE);
		mniFileHeader.setText("&File");	
		mniFileHeader.setMenu(mnuFile);
		
		final MenuItem mniEditHeader = new MenuItem(mnuMain, SWT.CASCADE);
		mniEditHeader.setText("&Edit");	
		mniEditHeader.setMenu(mnuEdit);
		
		final MenuItem mniOptionsHeader = new MenuItem(mnuMain, SWT.CASCADE);
		mniOptionsHeader.setText("&Options");	
		mniOptionsHeader.setMenu(mnuOptions);
		
		final MenuItem mniHelpHeader = new MenuItem(mnuMain, SWT.CASCADE);
		mniHelpHeader.setText("&Help");	
		mniHelpHeader.setMenu(mnuHelp);
	
		final MenuItem mniFileRestartSelServer = new MenuItem(mnuFile, SWT.PUSH);
		mniFileRestartSelServer.setText("Restart Selenium &Server");
		
		final MenuItem mniFileExit = new MenuItem(mnuFile, SWT.PUSH);
		mniFileExit.setText("E&xit");
		
		final MenuItem mniOptionsOptions = new MenuItem(mnuOptions, SWT.PUSH);
		mniOptionsOptions.setText("&Options...");
	
		//Event bindings for menus
		
		mniFileExit.addSelectionListener(new ExitListener(shMain));
		
		mniOptionsOptions.addSelectionListener(new OptionsListener(app, shMain));
		
		
	    
	    tabFolder.addPaintListener(new PaintListener() 
	    {
            public void paintControl(PaintEvent e) 
            {
            	tabFolder.setSize(shMain.getSize().x - 25, shMain.getSize().y - 70);
            	//cmpInteractive.setSize(640, 480);
            }
        });
		
		//Set the menu bar
	
		shMain.setMenuBar(mnuMain);
	
		return shMain;
    }
}
